package com.cloud.business.controller;

import com.alibaba.fastjson.JSON;
import com.cloud.business.entity.Spectrometer;
import com.cloud.iotdb.utils.IotDBDao;
import com.cloud.mqtt.util.CommonVariable;
import com.cloud.mqtt.util.DateUtils;
import com.cloud.mqtt.util.StringUtils;
import com.google.gson.JsonObject;
import lombok.extern.slf4j.Slf4j;
import org.fusesource.mqtt.client.BlockingConnection;
import org.fusesource.mqtt.client.MQTT;
import org.fusesource.mqtt.client.QoS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.*;

/**
 * @program: mqtt
 * @description: 接口类
 * @author: Cloud
 * @create: 2021/10/12 17:17:27
 */
@RestController
@RequestMapping("tb/device")
@Slf4j
public class RealTimeData {


    @Autowired
    private IotDBDao iotDBDao;

    // 有很多个网关采集到了的数据同时发到mqtt这边，然后，我拿到数据后把数据存储到iotdb中，然后，当有人调用getDevicesData(Get)接口时，我就从iotdb中取出最新的数据，根据设备号取出对应的
    // 线体数据，然后将数据封装成一段以json为格式的数据返回给labview，接着labview将数据进行分析，分析完了之后，将数据通过analyzedData接口(Post请求的方式)，返回给我，然后我又将数据存储
    // 起来，以方便后面对数据进行做应用。
    // 应用功能：1.能够查询到传过来和接收到的数据（历史日志记录），2.实时数据的展示，3.另外是需要对应表，4....
    @GetMapping("/getDevicesData")
    public String getDevicesData() {
        List<String> deviceList = new ArrayList<>();
        deviceList.add("A");
        deviceList.add("B");
        deviceList.add("C");
        deviceList.add("D");
        deviceList.add("E");
        Map<String, Object> aLastData = iotDBDao.getLastData("root.device.getAction.A", null, null);
        Map<String, Object> bLastData = iotDBDao.getLastData("root.device.getAction.B", null, null);
        Map<String, Object> cLastData = iotDBDao.getLastData("root.device.getAction.C", null, null);
        Map<String, Object> dLastData = iotDBDao.getLastData("root.device.getAction.D", null, null);
        Map<String, Object> eLastData = iotDBDao.getLastData("root.device.getAction.E", null, null);
        return "{\n" +
                "    \"data\": [\n" +
                "        {\n" +
                "            \"DeviceName\": \"GW01\",\n" +
                "            \"CavityData\": [\n" +
                "                {\n" +
                "                    \"CavityNumber\": \"A\",\n" +
                "                    \"GasState\": "+aLastData.get("status")+",\n" +
                "                    \"GasStateTime\": \""+aLastData.get("GasStateTime")+"\",\n" +
                "                    \"Reserve1\": \"1\",\n" +
                "                    \"Reserve2\": \"1\",\n" +
                "                    \"Reserve3\": 1,\n" +
                "                    \"Reserve4\": 1,\n" +
                "                },\n" +
                "                {\n" +
                "                    \"CavityNumber\": \"B\",\n" +
                "                    \"GasState\": "+bLastData.get("status")+",\n" +
                "                    \"GasStateTime\": \""+bLastData.get("GasStateTime")+"\",\n" +
                "                    \"Reserve1\": \"1\",\n" +
                "                    \"Reserve2\": \"1\",\n" +
                "                    \"Reserve3\": 1,\n" +
                "                    \"Reserve4\": 1,\n" +
                "                },\n" +
                "                {\n" +
                "                   \"CavityNumber\": \"C\",\n" +
                "                    \"GasState\": "+cLastData.get("status")+",\n" +
                "                    \"GasStateTime\": \""+cLastData.get("GasStateTime")+"\",\n" +
                "                    \"Reserve1\": \"1\",\n" +
                "                    \"Reserve2\": \"1\",\n" +
                "                    \"Reserve3\": 1,\n" +
                "                    \"Reserve4\": 1,\n" +
                "                },\n" +
                "                {\n" +
                "                    \"CavityNumber\": \"D\",\n" +
                "                    \"GasState\": "+dLastData.get("status")+",\n" +
                "                    \"GasStateTime\": \""+dLastData.get("GasStateTime")+"\",\n" +
                "                    \"Reserve1\": \"1\",\n" +
                "                    \"Reserve2\": \"1\",\n" +
                "                    \"Reserve3\": 1,\n" +
                "                    \"Reserve4\": 1,\n" +
                "                },\n" +
                "                {\n" +
                "                   \"CavityNumber\": \"E\",\n" +
                "                    \"GasState\": "+eLastData.get("status")+",\n" +
                "                    \"GasStateTime\": \""+eLastData.get("GasStateTime")+"\",\n" +
                "                    \"Reserve1\": \"1\",\n" +
                "                    \"Reserve2\": \"1\",\n" +
                "                    \"Reserve3\": 1,\n" +
                "                    \"Reserve4\": 1,\n" +
                "                }\n" +
                "            ]\n" +
                "        }\n" +
                "    ]\n" +
                "}";
    }

    @PostMapping("/analyzedData")
    public String analyzedData(@RequestBody Map<String,Object> map){
        if (StringUtils.isEmpty(map)) return "没有接收到数据！";
        log.info("传回来的数据是{}",map);
        long time = new Date().getTime();
        Object data = map.get("data");
        List specList = (List<Object>)data;
        Map<String,Object> params = (HashMap<String,Object>)specList.get(0);
        List<Map<String,Object>> cavityData = (ArrayList<Map<String,Object>>)params.get("CavityData");
        for (Map<String, Object> cavityDatum : cavityData) {
            cavityDatum.remove("Reserve1");
            cavityDatum.remove("Reserve2");
            cavityDatum.remove("Reserve3");
            cavityDatum.remove("Reserve4");
            cavityDatum.remove("Reserve5");
            cavityDatum.remove("Reserve6");
            cavityDatum.remove("Reserve7");
        }

        List<String> deviceList = new ArrayList<>();
        deviceList.add("A");
        deviceList.add("B");
        deviceList.add("C");
        deviceList.add("D");
        deviceList.add("E");

        iotDBDao.batchInset("root.device.postAction",deviceList,time,cavityData,0);
        log.info(map+"===>iotdb,插入数据库成功~");
        return "success!";
    }
}
